﻿//-----------------------------------------------------------------------
// <copyright file="ProcessPerfCounterValueProvider.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>29/03/2013</date>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics;

namespace ConsoleHoster.Model
{
	internal class ProcessPerfCounterValueProvider : IPerformanceCounterValueProvider
	{
		private readonly PerformanceCounterInfo counterInfo;
		private readonly string processName;
		private readonly PerformanceCounter perfCounter;

		public ProcessPerfCounterValueProvider(PerformanceCounterInfo argCounterInfo, string argProcessName)
		{
			if (argCounterInfo == null)
			{
				throw new ArgumentNullException("argCounterInfo");
			}

			if (String.IsNullOrWhiteSpace(argProcessName))
			{
				throw new ArgumentException("Invalid process name", "argProcessName");
			}

			this.counterInfo = argCounterInfo;
			this.processName = argProcessName;

			this.perfCounter = new PerformanceCounter(this.counterInfo.Category, this.counterInfo.Name, this.processName, true);
			this.perfCounter.NextValue();
		}

		public int GetCounterValue()
		{
			float tmpResult = this.perfCounter.NextValue();
			return (int)tmpResult;
		}

		public void Dispose()
		{
			this.perfCounter.Dispose();

		}
	}
}
